package com.open.capacity.sign.service.impl;
import com.open.capacity.common.model.*;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.sign.Util.ApplicationContextUtil;
import com.open.capacity.sign.dao.DayoffDao;
import com.open.capacity.sign.dao.SignDao;
import com.open.capacity.sign.service.SignService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ScheduledTask implements Job {

//    @Autowired
//    private SignDao signDao;
    public static String nowdate=getSimpleNowDateStr();
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //用于测试
        System.out.println("hello");
        //执行的任务
        //执行的任务1为task1：生成签到名单表
        createSignsTable();
        System.out.println("根据检测生成对应签到名单表成功");
        //执行的任务2为task2：检测临时放假表中的异常；并修复
        editDayoffsException();
        System.out.println("根据检测修复异常成功");
    }

    //任务2
    private void editDayoffsException() {
        //遍历所有的有效的异常数据
        List<Dayoff> dayoffs=getDayoffsException();
        for (Dayoff d:dayoffs){
            //修改签到数据表中的数据
            //进行放假修正,更新签到表中当天的签到放假状态
            String datetime=d.getTime();
            boolean success=updateSignState(datetime);
            if(success) {
                //将该条数据的异常设置为0
                int exState = 0;
                Dayoff dayoff = new Dayoff();
                dayoff.setId(d.getId());
                dayoff.setExState(exState);

                updateDayoffException(dayoff);
            }else{}
        }
    }

    //修改异常状态日期下签到表中的数据
    private boolean updateSignState(String datetime) {
        //根据时间获取所有在签到表下的成员签到数据
        List<Sign> signs=getSignsByTime(datetime);
        int res=1;
        for(Sign s:signs){
            int state=5;
            Sign sign = new Sign();
            sign.setId(s.getId());
            sign.setIstate(state);
            sign.setOstate(state);
            res=updateSignState(sign);
        }
        boolean result=(res!=0);
        return result;
    }

    //修改签到表中的状态
    private int updateSignState(Sign sign) {
        SignDao signDao = (SignDao) ApplicationContextUtil.getBean("signDao");
        int res=signDao.updateSignState(sign);
        return res;
    }

    //根据时间参数获取
    private List<Sign> getSignsByTime(String datetime) {
        SignDao signDao = (SignDao) ApplicationContextUtil.getBean("signDao");
        Sign sign=new Sign();
        sign.setTime(datetime);
        List<Sign> signs=signDao.getSignsByTime(sign);
        return signs;
    }

    //修改异常状态
    private void updateDayoffException(Dayoff dayoff) {
        DayoffDao dayoffDao = (DayoffDao) ApplicationContextUtil.getBean("dayoffDao");
        dayoffDao.editDayoffExState(dayoff);
    }

    //获取到所有的有效异常数据列
    private List<Dayoff> getDayoffsException() {
        DayoffDao dayoffDao = (DayoffDao) ApplicationContextUtil.getBean("dayoffDao");
        List<Dayoff> dayoffs=dayoffDao.getDayoffsException();
        return dayoffs;
    }

    //生成签到名单表
    private void createSignsTable() {
        //获取当前时间->判定当前是否为请假时间-》设置状态参数-》获取成员名单-》逐一添加
//        String nowDate=getSimpleNowDateStr();
        //判定当前是否为临时请假时间
        boolean ifDayoff=checkedIfDayoff();
        //初始化类型属性状态参数
        int state=0;
        if(ifDayoff){
            state=5;
        }
        List<SysUser> sysUsers =getUsers();
        for (SysUser s : sysUsers) {
//        逐个绑定类型参数
            Sign sign = new Sign();
            sign.setUserId(s.getId());
            sign.setTime(nowdate);
            sign.setIstate(state);
            sign.setOstate(state);
            //执行方法：添加签到类型数据
            addSignData(sign);
        }
    }

    //添加签到类型数据
    private void addSignData(Sign sign) {
        SignDao signDao = (SignDao) ApplicationContextUtil.getBean("signDao");
        signDao.setSignState(sign);
    }

    //获取所有人的名单信息
    private List<SysUser> getUsers() {
        SignDao signDao = (SignDao) ApplicationContextUtil.getBean("signDao");
        List<SysUser> sysUsers=signDao.getUsers();
        return sysUsers;
    }

    //检测是否为放假时间
    private boolean checkedIfDayoff() {
        DayoffDao dayoffDao = (DayoffDao) ApplicationContextUtil.getBean("dayoffDao");
        int count=dayoffDao.findDayoffByDate(nowdate);
        boolean isHave=(count!=0);
        return isHave;
    }

    //获取当前时间
    private static String getSimpleNowDateStr() {
        Date d = new Date();
        SimpleDateFormat nowDate = new SimpleDateFormat("yyyy-MM-dd");
        String time = nowDate.format(d);
        return time;
    }


}